<!DOCTYPE html>
<html>
    <head>
        <meta charset="utf-8">
        <meta name="viewport" content="maximum-scale=1.0,minimum-scale=1.0,user-scalable=0,width=device-width,initial-scale=1.0"/>
        <meta name="format-detection" content="telephone=no" />
        <title>商品详情</title>
        <link rel="stylesheet" type="text/css" href="../css/aui.css" />
        <link rel="stylesheet" type="text/css" href="../css/aui-flex.css" />
        <link rel="stylesheet" type="text/css" href="../css/style.css" />
        <style>
            body{
                font-size: 14px;
            }
            #footer{
                text-align: center;
                display: none;
            }
            #footer .aui-flex-item-2,
            #footer .aui-flex-item-3{
                height: 55px;
            }
            #footer .aui-flex-item-2{
                padding-top:5px;
                font-size: 13px;
                color:#777;
            }
            #footer .aui-flex-item-3{
                font-size: 15px;
                color: #fff;
                height: 55px;
                line-height: 55px;
            }
            .hasFavored .aui-iconfont{
                color:#ed4632 !important;
            }
            .myMessage {
                position: relative;
            }
            .myMessage:before {
                content: "";
                position: absolute;
                width: 8px;
                height: 8px;
                border-radius: 50%;
                background: red;
                right: -6px;
                top: -4px;
            }
        </style>
    </head>
    <body>
        <header class="aui-bar aui-bar-nav aui-border-b">
          <div class="aui-pull-left aui-iconfont aui-icon-left icon-left-coler" tapmode onclick="closeWin()"></div>
          <div class="aui-title">
            <div class="aui-tab">
                <ul class="aui-tab-nav" id="headerTab">
                    <li class="active" data-type="1" tapmode>商品</li>
                    <li data-type="2" tapmode>详情</li>
                </ul>
            </div>
          </div>
        </header>
        <input type="hidden" name="selected_specs" value="">

        <footer class="aui-nav" id="footer">
            <div class="aui-flex-col">
                <div class="aui-flex-item-2 aui-border-r" id="go2Home">
                    <span class="aui-iconfont aui-icon-home"></span>
                    <div>店铺</div>
                </div>
                <div class="aui-flex-item-2 aui-border-r" id="go2Chat">
                    <span class="noMessage"><span class="aui-iconfont aui-icon-service"></span></span>
                    <div>客服</div>
                </div>
                <div class="aui-flex-item-2 aui-border-r" id="addFavor">
                    <span class="aui-iconfont aui-icon-favor"></span>
                    <div class="text">收藏</div>
                </div>
                <div class="aui-flex-item-3">
                    <div class="aui-bg-warning" id="add2Cart">加入进货单</div>
                </div>
                <div class="aui-flex-item-3">
                    <div class="aui-bg-danger" id="buyNow">立即订购</div>
                </div>
            </div>
        </footer>
        
    </body>
    <script type="text/javascript" src="../script/api.js"></script>
    <script type="text/javascript" src="../script/common.js"></script>
    <script type="text/javascript" src="../script/jquery-1.11.3.js"></script>
    <script type="text/javascript">
        var isLock=false,itemid;
        var specData,itemData;
        var companyName,localUserId;
        apiready = function() {
            api.parseTapmode();
            itemid = api.pageParam.itemid;

            var header = $api.dom('header');
            $api.fixIos7Bar(header);

            loadData(itemid);

            //监听清除消息数
            api.addEventListener({
                name: 'clearChatUserCount'
            }, function(ret, err) {
                if (ret&&ret.value) {
                    var targetUserId=ret.value.userId;
                    if(targetUserId==localUserId){
                        $('.noMessage').removeClass('myMessage');
                    }
                } 
            });
        }

        //获取商品信息
        function loadData(itemid) {
            if(isLock)return;
            isLock=true;

            showLoading();
            getStorageAll();

            var data = {};
            var url = 'mall/index/goodsId/' + itemid + /userid/ + user_id;
            ajaxRequest(url, 'get', data, function (ret, err) {
                if (ret.status == 1) {
                    itemData=ret.msg;
                    companyName=ret.msg.company.company;

                    if(user_id!=ret.msg.company.userid){
                        $('#footer').show();
                    }
                    showInnerPage(ret.msg);

                    //头部转换
                    $('#headerTab li').unbind('click').bind('click',function(){
                        var type=$(this).attr('data-type');
                        $('#headerTab .active').removeClass('active');
                        $(this).addClass('active');

                        api.execScript({
                            frameName:'proInfo_frm',
                            script:'changeHeader('+type+')'
                        });
                    });

                    //转到首页
                    $('#go2Home').unbind('click').bind('click',function(){
                        go2Shop(ret.msg.company.userid);
                    });

                    //客服中心
                    $('#go2Chat').unbind('click').bind('click',function(){
                        openchat(ret.msg.company.userid,ret.msg.company.thumb,ret.msg.company.company,false,true,itemid)
                    });

                    //检查聊天通知
                    localUserId=ret.msg.company.userid;
                    if(hasNewMsgByUid(localUserId)){
                        $('.noMessage').addClass('myMessage');
                    }

                    //收藏
                    if(ret.msg.iscollect=='1')$('#addFavor').addClass('hasFavored').find('.text').text('已收藏');
                    $('#addFavor').unbind('click').bind('click',function(){
                        var $this=$(this);
                        getStorageAll();

                        if (!user_id) {
                            openTo('loginPass_win');
                            return;
                        }

                        if($(this).hasClass('hasFavored')){
                            //取消收藏动作
                            data = {
                                userName: user_name,
                                type: 1,
                                id: itemid,
                            };
                            url = "User/collectionCancel";
                            ajaxRequest(url, 'post', data, function (ret, err) {
                                if (ret.status == 1) {
                                    $this.removeClass('hasFavored');
                                    $this.find('.text').text('收藏');
                                    ret.msg='取消收藏成功';

                                    //发送监听
                                    api.sendEvent({
                                        name: 'updateItemFavoriteStatus',
                                        extra: {
                                            itemid: itemid,
                                            status:0
                                        }
                                    });
                                }
                                api.toast({
                                    msg: ret.msg,
                                    duration: 2000,
                                    location: 'bottom'
                                });
                            });
                        }else{
                            //收藏动作
                            var data = {};
                            var url = 'factory/collect/userid/' + user_id + '/type/mall/id/' + itemid;
                            ajaxRequest(url, 'get', data, function (ret, err) {
                                if (ret.msg == 1) {
                                    $this.find('.text').text('已收藏');
                                    $this.addClass('hasFavored');
                                    ret.msg='添加收藏成功';

                                    //发送监听
                                    api.sendEvent({
                                        name: 'updateItemFavoriteStatus',
                                        extra: {
                                            itemid: itemid,
                                            status:1
                                        }
                                    });
                                }
                                api.toast({
                                    msg: ret.msg,
                                    duration: 2000,
                                    location: 'bottom'
                                });
                            });
                        }
                    });

                    //加入进货单
                    $('#add2Cart').unbind('click').bind('click',function(){
                        // add2Cart();
                        showAttribute();
                    });

                    //立即订购
                    $('#buyNow').unbind('click').bind('click',function(){
                        // go2Order();
                        showAttribute();
                    });

                    //获取商品规格数据
                    loadSpecData();
                } else {
                    api.toast({
                        msg: ret.msg,
                        duration: 2000,
                        location: 'bottom'
                    });
                }
                hideLoading();
                isLock=false;
            });
        }

        function showInnerPage(data){
            var header = $api.dom('header');
            var footer = $api.dom('footer');
            $api.fixIos7Bar(header);

            var header_h = $api.offset(header).h;
            var footer_h = $api.offset(footer).h;
            var body_h = $api.offset($api.dom('body')).h - header_h;
            api.openFrame({
                name : 'proInfo_frm',
                url : 'proInfo_frm.html',
                rect : {
                    x : 0,
                    y : header_h,
                    w : 'auto',
                    h : body_h-footer_h
                },
                pageParam : {
                    itemid : itemid,
                    data:data
                },
                bounces : true,
                vScrollBarEnabled : false,
                hScrollBarEnabled : false
            });
        }

        function loadSpecData(){
            url = 'mall/spec/goodsId/' + itemid;
            ajaxRequest(url, 'get', {}, function (ret, err) {
                if(ret.status==1){
                    specData=ret.msg;
                }else{
                    api.toast({
                        msg: ret.msg,
                        duration: 2000,
                        location: 'bottom'
                    });
                }
            });
        }

        function showAttribute(needToast){
            getStorageAll();
            //检测用户名
            if (!user_id) {
                openTo('loginPass_win');
                return false;
            }

            if(specData){
                api.openFrame({
                    name: 'proAttribute_frm',
                    url: './proAttribute_frm.html',
                    animation:{
                        type:"none"             
                    },
                    pageParam:{
                        itemData:itemData,
                        specData:specData,
                        selectedSpecs:$('input[name="selected_specs"]').val()
                    }
                });
            }else{
                loadSpecData();
            }

            if (needToast) {
                api.toast({
                    msg: '请选择库存',
                    duration: 2000,
                    location: 'bottom'
                });
            }
        }

        //订购
        function go2Order(needToast){
            var specs=$('input[name="selected_specs"]').val();
            if(specs){
                $api.setStorage('_itemId',itemid);
                $api.setStorage('_str',specs);
                $api.setStorage('_companyname',companyName);

                api.openWin({
                    name: 'orderComfirm',
                    url: 'orderComfirm.html',
                    pageParam: {
                        itemid: itemid
                    },
                    vScrollBarEnabled:false
                });
            }else{
                showAttribute(needToast);
            }
        }

        //加入购物车
        function add2Cart(needToast){
            var specs=$('input[name="selected_specs"]').val();
            if(specs){
                getStorageAll();
                var data = {
                    goodsId: itemid,
                    str: specs,
                    userId: user_id,
                };
                var url ='Mallcart/addCart';
                ajaxRequest(url, 'post', data, function (ret, err) {
                    if (ret.status == 1) {
                        //发送监听
                        api.sendEvent({
                            name : 'updateCart',
                            extra : {
                                key : 'true'
                            }
                        });

                        //res
                        api.closeFrame({
                            name: 'proAttribute_frm'
                        });

                        api.toast({
                            msg: '已加入进货单',
                            duration: 2000,
                            location: 'bottom'
                        });
                    } else {
                        api.toast({
                            msg: ret.msg,
                            duration: 2000,
                            location: 'bottom'
                        });
                    }
                });      
            }else{
                showAttribute(needToast);
            }
        }

        //更新隐藏规格
        function updateHiddenInput(str){
            $('input[name="selected_specs"]').val(str);
        }
    </script>
</html>
